﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using KOF.Helpers;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.ComponentModel;
using Microsoft.Phone.Net.NetworkInformation;

namespace KOF.Views
{
    public partial class Favorites : PhoneApplicationPage
    {
        public Favorites()
        {
            //This Code checks internet Connection
            if (NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None)
            {

            }
            else
            {
                MessageBox.Show("Please check your internet connection!");
            }

            InitializeComponent();
  
                       
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
              
              // When We Press Back Button This Code will save favorite list in local application 
                if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
                {
                    List.SelectedIndex = -1;
                    if (IsolatedStorageSettings.ApplicationSettings.Contains("FavoritesList"))
                    {
                        List<KOF.Helpers.ReleaseResult> lstFav = (List<KOF.Helpers.ReleaseResult>)IsolatedStorageSettings.ApplicationSettings["FavoritesList"];
                        if (lstFav != null)
                        {
                          

                            List.ItemsSource = lstFav;

                            
                        }
                    }
                }

               
                base.OnNavigatedTo(e);
                
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

      //Redirect To Setting Screen
        private void btnSettings_Click(object sender, EventArgs e)
        {
            try
            {
                this.NavigationService.Navigate(new Uri("/Views/Settings.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        //REdirect to Release Screen
        private void btnReleases_Click(object sender, EventArgs e)
        {
            try
            {


                IsolatedStorageSettings.ApplicationSettings["loader"] = false;
                this.NavigationService.Navigate(new Uri("/Views/MainPage.xaml", UriKind.Relative));
              
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        //Redirect To News Screen
        private void btnNews_Click(object sender, EventArgs e)
        {
            try
            {
            this.NavigationService.Navigate(new Uri("/Views/News.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        // On list Click Redirect To ShoeDetails page
        private void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
            if (e.AddedItems.Count > 0)
            {
                ReleaseResult item = e.AddedItems[0] as ReleaseResult;
                (App.Current as App).lstImages = item.iphone_images;

                KOF.Helpers.ReleaseResult temp = (from itm in (App.Current as App).lstReleaseResult
                                                  where itm.id == item.id
                                                  select itm).FirstOrDefault();
                if (temp == null)
                {
                    (App.Current as App).lstReleaseResult.Add(item);
                }
                
               
                this.NavigationService.Navigate(new Uri(String.Format("/Views/ShoeDetails1.xaml?id={0}", item.id), UriKind.Relative));

            }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }
}